//
//  StarView.m
//  UI_Lesson17三方类
//
//  Created by dllo on 15/9/8.
//  Copyright (c) 2015年 蓝鸥科技. All rights reserved.
//

#import "StarView.h"

@implementation StarView

- (instancetype)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    
    if (self) {
        for (NSInteger i = 0; i < 5; i++) {
            UIImageView *imageView = [[UIImageView alloc] init];
            [self addSubview:imageView];
            
        }
    }
    return self;
}


- (void)awakeFromNib
{
    for (NSInteger i = 0; i < 5; i++) {
        UIImageView *imageView = [[UIImageView alloc] init];
        [self addSubview:imageView];
    }
}

- (void)layoutSubviews
{
    [super layoutSubviews];
    
    CGFloat width = self.bounds.size.width / 5;
    CGFloat height = self.bounds.size.height;
    // 有几个满星
    NSInteger fullStarNum = self.starNum / 10;
    
    // 是否有半颗星
    BOOL halfStar = (self.starNum % 10 != 0);
    for (NSInteger i = 0; i < 5; i++) {
        UIImageView *imageView = [self.subviews objectAtIndex:i];
        imageView.frame = CGRectMake(width * i, 0, width, height);
        
        if (i < fullStarNum) {
            imageView.image = [UIImage imageNamed:@"starfull"];
        }
        else if (i == fullStarNum && halfStar == YES) {
            imageView.image = [UIImage imageNamed:@"starhalf"];
        }else{
            imageView.image = [UIImage imageNamed:@"starempty"];
        }
    }
}










/*
// Only override drawRect: if you perform custom drawing.
// An empty implementation adversely affects performance during animation.
- (void)drawRect:(CGRect)rect {
    // Drawing code
}
*/

@end
